package controller.role;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Role;
import service.RoleService;
import service.impl.RoleServiceImpl;
import utils.AJAXResult;
import utils.Page;
import utils.ResponseBodyUtil;

public class PageQuery extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private RoleService roleService = new RoleServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		AJAXResult result = new AJAXResult();
		
		int pageno = Integer.parseInt(request.getParameter("pageno"));
		int pagesize = Integer.parseInt(request.getParameter("pagesize"));
		String queryText = request.getParameter("queryText");
		
		try {			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("start", (pageno-1)*pagesize);
			map.put("size", pagesize);
			map.put("queryText", queryText);
			
			List<Role> roles = roleService.pageQueryData( map );

			int totalsize = roleService.pageQueryCount( map );
			int totalno = 0;
			if ( totalsize % pagesize == 0 ) {
				totalno = totalsize / pagesize;
			} else {
				totalno = totalsize / pagesize + 1;
			}

			Page<Role> rolePage = new Page<Role>();
			rolePage.setDatas(roles);
			rolePage.setTotalno(totalno);
			rolePage.setTotalsize(totalsize);
			rolePage.setPageno(pageno);
			
			result.setData(rolePage);
			result.setSuccess(true);
		} catch ( Exception e ) {
			e.printStackTrace();
			result.setSuccess(false);
		}
		
		ResponseBodyUtil.sendJsonObject(response, result);
	}
}
